<?php
/**
 * --------------------------------------------------------------
 * 基于 uploadifive 控件上传类
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

class KfzUpload {

    private $extension = ''; // 文件扩展名
    private $fileTypes = ''; // 文件所属类型

    /**
     * 构造方法
     */
    public function __construct()
    {

    }

    /**
     * 验证文件
     * @author zd <kfzphp@126.com>
     * @since  2018-01-19
     * @return [type]     [description]
     */
    private function check()
    {
        // 验证TOKEN
        // $verifyToken = md5('unique_salt' . $_POST['timestamp']);
        // ($_POST['token'] == $verifyToken)

        // 验证文件类型
        $fileParts = pathinfo($_FILES['Filedata']['name']);
        $fileTypes = self::getType($fileParts['extension']);
        if(!$fileTypes) {
            return array('status' => 0, 'info' => '上传文件类型不允许');
        }
        $this->extension = $fileParts['extension'];
        $this->fileTypes = $fileTypes;

        // 验证文件大小
        $fileSize = $_FILES['Filedata']['size'];
        $maxSize = $GLOBALS['_CFG']['UPLOAD_MAX_SIZE'];    // 允许的文件扩展名
        if($fileSize > $maxSize) {
            return array('status' => 0, 'info' => '上传文件的大小超过了系统限制');
        }
    }

    /**
     * 返回支持的文件类型一维数组
     * @author zd <kfzphp@126.com>
     * @since  2018-01-19
     * @param  [type]     $types [description]
     * @return [type]            [description]
     */
    private function getTypes($types)
    {
        if(empty($types)) {
            return false;
        }

        $array = [];
        foreach($types as $type) {
            foreach($type as $v) {
                $array[] = $v;
            }
        }
        return $array;
    }

    /**
     * 获取扩展名所属文件类型
     * @author zd <kfzphp@126.com>
     * @since  2018-01-19
     * @param  [type]     $ext [description]
     * @return [type]          [description]
     */
    private function getType($ext)
    {
        // 允许的文件扩展名
        $types = $GLOBALS['_CFG']['UPLOAD_ALLOW_TYPE'];
        if($types) {
            foreach($types as $type => $exts) {
                foreach($exts as $val) {
                    if($val == $ext) {
                        return $type;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 上传文件名
     * @author zd <kfzphp@126.com>
     * @since  2018-01-19
     * @param  [type]     $ext [文件扩展名]
     * @return [type]          [description]
     */
    private function getFile()
    {
        $name = date('YmdHis', TIMESTAMP) . '_' . create_random(6) . '.' . $this->extension;
        $dir = UPLOAD_PATH . '/' . $this->fileTypes . '/' . date('Y', TIMESTAMP) . '/' . date('m', TIMESTAMP) . '/' . date('d', TIMESTAMP);
        if(!is_dir($dir)) {
            $res = mkdir(iconv("UTF-8", "GBK", $dir), 0777, true);
            if(!$res) {
                return array('status' => 0, 'info' => '创建目录权限错误');
            }
        }
        $path = $dir . '/' . $name;
        return [
            'name' => $name, // 文件名
            'dir' => $dir,    // 文件目录名
            'path' => $path,   // 文件完整路径
            'save' => my_ltrim($path),
        ];
    }

    /**
     * 上传文件
     * @author zd <kfzphp@126.com>
     * @since  2018-01-19
     * @return [type]     [description]
     */
    public function upload()
    {
        // 判断上传
        if(!$_FILES) {
            return array('status' => 0, 'info' => '文件错误');
            exit;
        }
        // 检查上传文件
        self::check();
        // 保存参数
        $tempFile = $_FILES['Filedata']['tmp_name'];  // 临时文件
        $uploadPath = self::getFile();
        $targetFile = $uploadPath['path'];   // 文件保存目录
        // 保存文件
        move_uploaded_file($tempFile, $targetFile);
        // pt($uploadPath);
        // 返回成功
        return array('status' => 1, 'info' => '上传成功', 'file' => $uploadPath['save']);
    }



}
